import Vue from "vue";
import VueRouter from "vue-router";
import Login from "../views/Login.vue";
import controller from "@/views/controller";
import SwitchTest from "@/views/controller/SwitchTest";
// import Index from "@/views/controller/index";
import State from "@/store/index";

Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    redirect: "Login",
  },
  {
    path: "/Login",
    name: "Login",
    component: Login,
  },

  {
    path: "/SwitchTest",
    name: "SwitchTest",
    meta: {
      title: "模式切换",
      role: [2],
    },
    component: SwitchTest,
  },
  {
    path: "/controller",
    name: "controller",
    redirect: "/home",
    meta: {
      title: "系统首页",
      role: [2],
    },
    component: controller,
    children: [
      {
        path: "/index",
        name: "Index",
        meta: {
          title: "系统首页",
          role: [2],
        },
        component: () =>
          import(/* webpackChunkName: "Index" */ "@/views/controller/index"),
      },
      {
        path: "/home",
        name: "home",
        meta: {
          title: "系统首页",
          role: [2],
        },
        component: () =>
          import(/* webpackChunkName: "home" */ "@/views/controller/home"),
      },
      {
        path: "/Adinfo",
        name: "Adinfo",
        meta: {
          title: "到场人数",
          role: [2],
        },
        component: () =>
          import(/* webpackChunkName: "Adinfo" */ "@/views/controller/Adinfo"),
      },
      {
        path: "/Exama",
        name: "Exama",
        meta: {
          title: "考试管理",
          role: [2],
        },
        component: () =>
          import(/* webpackChunkName: "Exama" */ "@/views/controller/Exama"),
      },

      {
        path: "/monitor",
        name: "Monitor",
        meta: {
          title: "进程展示",
          keepAlive: true,
          role: [2],
        },
        component: () =>
          import(
            /* webpackChunkName: "Monitor" */ "@/views/controller/monitor"
          ),
      },
      {
        path: "/monitorVideo",
        name: "monitorVideo",
        meta: {
          title: "视频监控",
          role: [2],
        },
        component: () =>
          import(
            /* webpackChunkName: "monitorVideo" */ "@/views/controller/monitorVideo"
          ),
      },

      {
        path: "/information",
        name: "information",
        meta: {
          title: "信息录入",
          role: [2],
        },
        component: () =>
          import(
            /* webpackChunkName: "information" */
            "@/views/controller/information"
          ),
      },
      // {
      //   path: "/getNavigator",
      //   name: "getNavigator",
      //   meta: {
      //     title: "调用摄像头",
      //   },
      //   component: () =>
      //     import(
      //       /* webpackChunkName: "information" */
      //       "@/views/controller/getNavigator"
      //     ),
      // },
      {
        path: "/usertrol",
        name: "usertrol",
        meta: {
          title: "用户管理",
          role: [1, 2],
        },
        component: () =>
          import(
            /* webpackChunkName: "usertrol" */
            "@/views/controller/usertrol"
          ),
      },
      {
        path: "/studentMaget",
        name: "studentMaget",
        meta: {
          title: "学生管理",
          keepAlive: true,
          role: [2],
        },
        component: () =>
          import(
            /* webpackChunkName: "systemAdinfo" */
            "@/views/controller/studentMaget"
          ),
      },
      {
        path: "/DeviceManagement",
        name: "DeviceManagement",
        meta: {
          title: "设备管理",
          role: [1, 2],
        },
        component: () =>
          import(
            /* webpackChunkName: "DeviceManagement" */
            "@/views/controller/DeviceManagement"
          ),
      },
      {
        path: "/train",
        name: "Train",
        meta: {
          title: "培训管理",
          role: [2],
        },
        component: () =>
          import(/* webpackChunkName: "train" */ "@/views/controller/train"),
      },
      {
        path: "/grade",
        name: "Grade",
        meta: {
          title: "考试分数",
          role: [2],
        },
        component: () =>
          import(/* webpackChunkName: "grade" */ "@/views/controller/grade"),
      },
      {
        path: "/TrainData",
        name: "TrainData",
        meta: {
          title: "考生数据",
          role: [2],
        },
        component: () =>
          import(
            /* webpackChunkName: "TrainData" */ "@/views/controller/TrainData"
          ),
      },
      {
        path: "/systemlog",
        name: "systemlog",
        meta: {
          title: "系统日志",
          role: [2],
        },
        component: () =>
          import(
            /* webpackChunkName: "systemlog" */ "@/views/controller/systemlog"
          ),
      },
      {
        path: "/messagecenter",
        name: "MessageCenter",
        meta: {
          title: "消息中心",
          role: [2],
        },
        component: () =>
          import(
            /* webpackChunkName: "systemlog" */ "@/views/controller/MessageCenter"
          ),
      },
      {
        path: "/403",
        name: "403",
        meta: {
          title: "403",
          role: [1, 2, 3, 4, 5],
        },
        component: () => import(/* webpackChunkName: "403" */ "@/views/403"),
      },
    ],
  },
];

const router = new VueRouter({
  mode: "history",
  routes,
});
const routerPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return routerPush.call(this, location).catch((error) => error);
};

// 导航守卫
// 使用 router.beforeEach 注册一个全局前置守卫，判断用户是否登陆
router.beforeEach((to, from, next) => {
  if (to.path === "/Login") {
    next();
  } else {
    let token = localStorage.getItem("Authorization");
    if (!token) {
      next("/Login");
    } else {
      let role = Number(State.state.role);
      let inrole = to.meta.role;
      if (inrole.includes(role) || role == 1) {
        next();
      } else {
        next("/403");
      }
    }
  }
});

// export default new Router({
//   mode: 'history', // 去掉url中的#
//   scrollBehavior: () => ({ y: 0 }),
//   routes: constantRoutes
// })

export default router;
